<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActiveRecord::AttributeMethods::Serialization::ClassMethods</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../../../css/github.css" type="text/css" media="screen" />
<script src="../../../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Module</span> 
            ActiveRecord::AttributeMethods::Serialization::ClassMethods 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../../../files/activerecord/lib/active_record/attribute_methods/serialization_rb.html">activerecord/lib/active_record/attribute_methods/serialization.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>S</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="ClassMethods.html#method-i-serialize">serialize</a>,
              </li>
            
              
              <li>
                <a href="ClassMethods.html#method-i-serialized_attributes">serialized_attributes</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    


    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-serialize">
            
              <b>serialize</b>(attr_name, class_name = Object)
            
            <a href="ClassMethods.html#method-i-serialize" name="method-i-serialize" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>If you have an attribute that needs to be saved to the database as an
object, and retrieved as the same object, then specify the name of that
attribute using this method and it will be handled automatically. The
serialization is done through YAML. If <code>class_name</code> is
specified, the serialized object must be of that class on retrieval or
<code>SerializationTypeMismatch</code> will be raised.</p>

<h4 id="method-i-serialize-label-Parameters">Parameters</h4>
<ul><li>
<p><code>attr_name</code> - The field name that should be serialized.</p>
</li><li>
<p><code>class_name</code> - Optional, class name that the object type should
be equal to.</p>
</li></ul>

<h4 id="method-i-serialize-label-Example">Example</h4>

<pre class="ruby"><span class="ruby-comment"># Serialize a preferences attribute.</span>
<span class="ruby-keyword">class</span> <span class="ruby-constant">User</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">serialize</span> :<span class="ruby-identifier">preferences</span>
<span class="ruby-keyword">end</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-serialize_source')" id="l_method-i-serialize_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/ec2b971118daaac06f13d9b416b952a779bc0fb5/activerecord/lib/active_record/attribute_methods/serialization.rb#L38" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-serialize_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/attribute_methods/serialization.rb, line 38</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">serialize</span>(<span class="ruby-identifier">attr_name</span>, <span class="ruby-identifier">class_name</span> = <span class="ruby-constant">Object</span>)
  <span class="ruby-identifier">include</span> <span class="ruby-constant">Behavior</span>

  <span class="ruby-identifier">coder</span> = <span class="ruby-keyword">if</span> [<span class="ruby-value">:load</span>, <span class="ruby-value">:dump</span>].<span class="ruby-identifier">all?</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span> <span class="ruby-identifier">class_name</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">x</span>) }
            <span class="ruby-identifier">class_name</span>
          <span class="ruby-keyword">else</span>
            <span class="ruby-constant">Coders</span><span class="ruby-operator">::</span><span class="ruby-constant">YAMLColumn</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">class_name</span>)
          <span class="ruby-keyword">end</span>

  <span class="ruby-comment"># merge new serialized attribute and create new hash to ensure that each class in inheritance hierarchy</span>
  <span class="ruby-comment"># has its own hash of own serialized attributes</span>
  <span class="ruby-keyword">self</span>.<span class="ruby-identifier">serialized_attributes</span> = <span class="ruby-identifier">serialized_attributes</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">attr_name</span>.<span class="ruby-identifier">to_s</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">coder</span>)
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-serialized_attributes">
            
              <b>serialized_attributes</b>
            
            <a href="ClassMethods.html#method-i-serialized_attributes" name="method-i-serialized_attributes" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns a hash of all the attributes that have been specified for
serialization as keys and their class restriction as values.</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-serialized_attributes_source')" id="l_method-i-serialized_attributes_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/ec2b971118daaac06f13d9b416b952a779bc0fb5/activerecord/lib/active_record/attribute_methods/serialization.rb#L20" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-serialized_attributes_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activerecord/lib/active_record/attribute_methods/serialization.rb, line 20</span>
        </pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    